package fr.n7.khome.view.renderer;
/* 
 * ColorRenderer.java (compiles with releases 1.2, 1.3, and 1.4) is used by 
 * TableDialogEditDemo.java.
 */

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;

import fr.n7.khome.view.ResultField;


import java.awt.Color;
import java.awt.Component;

public class ResultFieldRenderer extends JLabel implements TableCellRenderer {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5851321088470812631L;
	Border unselectedBorder = null;
	Border selectedBorder = null;
	boolean isBordered = true;

	public ResultFieldRenderer() {
		super();
		setOpaque(true); //MUST do this for background to show up.
		setEnabled(true);
		setHorizontalAlignment(JLabel.CENTER);
		unselectedBorder = BorderFactory.createEmptyBorder();
		selectedBorder = BorderFactory.createMatteBorder(1, 0, 1, 0, Color.blue);
	}

	public Component getTableCellRendererComponent(
			JTable table, Object o,
			boolean isSelected, boolean hasFocus,
			int row, int column) {
		if(o != null){
			if(o instanceof ResultField) {
				ResultField rf = (ResultField)o;
				setText(rf.getField().toString());
				setToolTipText(
							"id="+rf.getID().toString()
						+	", N="+rf.getNecessity()
						+	", gN="+rf.getGNecessity()
						+	", P="+rf.getLikelihood()
						+	", gP="+rf.getGLikelihood()
						+	", sumN="+rf.getSumNecessity()
						+	", sumP="+rf.getSumLikelihood());
				int n = (int)(145*rf.getNecessity());
				int l = (int)(145*rf.getLikelihood());
				int intensity = 255-(int)((rf.getFirstRowGNecessity()-rf.getGNecessity())*200);
				int r = 255-n;
				int g = 110+l;
				int b = 100;
				r = Math.max(0,Math.min(r,255))-(isSelected?50:0);
				g = Math.max(0,Math.min(g,255))-(isSelected?50:0);
				b = Math.max(0,Math.min(b,255))+(isSelected?50:0);
				setBackground(new Color(r,g,b,intensity));
				setForeground(new Color(0,0,0,127+intensity/2));
				if(isSelected){
					setBorder(selectedBorder);
				}else{
					setBorder(unselectedBorder);
				}
			}
		}
		return this;
	}
}
